import styled, { keyframes, createGlobalStyle, css } from 'styled-components'

// 样式复用
const flexColCenter = css`
  display: flex;
  flex-direction: column;
  justify-content: center;
  align-items: center;
`
const textHighlightColor = css`
  color: ${props => props.hightightColor ? props.hightightColor : 'white'};
`

const Wrap = styled.div`
  width: 100%;
  height: 100vh;
  ${flexColCenter}
  gap: 20px;
`

// css帧动画
const aniTag = keyframes`
  0% {
    opacity: 0;
    transform: translateY(-100%);
  }
  100% {
    opacity: 1;
    transform: translateY(0);
  }
`

const MyButton = styled.div`
  width: 400px;
  height: 80px;
  border:1px solid gray;
  border-radius: 10px;
  overflow: hidden;
  ${flexColCenter}
  position: relative;
  // 传参
  background-color: ${props => props.bgNormalColor ? props.bgNormalColor : 'blue'};
  transition: background-color 0.3s ease-in-out;
  // 伪元素
  &::after{
    content: 'after';
    height: 20px;
    font-size: 14px;
    line-height: 20px;
    padding:0 5px;
    border-radius: 0 0 6px 6px;
    position: absolute;
    top: 0;
    right: 10px;
    background-color: yellow;
    ${textHighlightColor}
    opacity: 0;
  }
  // 子孙选择器
  p {
    line-height: 24px;
    color: ${props => props.normalColor ? props.normalColor : 'black'};
    transition: color 0.3s ease-in-out;
  }
  // 当有全局样式的时候，使用&&提高局部样式优先级来覆盖全局样式
  && span{
    color: rgba(255,0,255,0.6);
  }
  // 类选择器
  .emphasize{
    text-decoration: underline;
  }
  // 伪类
  &:hover{
    background-color: ${props => props.bgHighlightColor ? props.bgHighlightColor : 'yellow'};
    p{
      ${textHighlightColor}
    }
    // css animation动画
    &::after{
      animation: ${aniTag} 0.4s ease-in-out forwards;
    }
  }
`

const MyButton1 = styled(MyButton)`
  border:4px solid gray;
  border-radius: 20px;
`

// 全局样式
const GlobalStyle = createGlobalStyle`
  *{
    padding: 0;
    margin: 0;
  }
  ${MyButton}{
    span{
      color: rgba(255,255,255,0.6);
      font-size: 12px;
      line-height: 20px;
    }
  }
`

export default function StyleComponentsTest() {
  return (
    <Wrap>
      <GlobalStyle />
      <MyButton 
        bgNormalColor="gray" 
        bgHighlightColor="white" 
        normalColor='rgba(255,255,255,0.6)' 
        hightightColor="red"
      >
          <p><strong className="emphasize">styled components</strong> 入门</p>
          <span>我是只小小鸟</span>
      </MyButton>
      <MyButton1 
        bgNormalColor="gray" 
        bgHighlightColor="white" 
        normalColor='rgba(255,255,255,0.6)' 
        hightightColor="red"
      >
          <p><strong className="emphasize">styled components</strong> 入门1</p>
          <span>我是只小小鸟1</span>
      </MyButton1>
    </Wrap>
  )
}